﻿using HrEFCore;
using HrEFCore.Models;
using HrIService;
using HrService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace HrUI.Controllers
{
    public class UserController : Controller
    {
        private readonly MyDbContext myDbContext;
        private readonly UserService userService;
		private readonly RolesService rolesService;

		public UserController(MyDbContext myDbContext, UserService userService,RolesService rolesService)
        {
            this.myDbContext = myDbContext;
            this.userService = userService;
			this.rolesService = rolesService;
		}
        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Login(string UName, string UPassWord)
        {
            if (string.IsNullOrEmpty(UName) || string.IsNullOrEmpty(UPassWord))
            {
                TempData["ErrorMessage"] = "请填写完整的登录名和密码";
                return RedirectToAction("Login");
            }

            int userId = userService.Login(UName, UPassWord);
            string UTrueName = userService.InquireName(userId);

            if (!string.IsNullOrEmpty(UTrueName))
            {
                TempData["UTrueName"] =UTrueName;
                HttpContext.Session.SetString("UTrueName", UTrueName);
            }
			int getRolesID = rolesService.GetRolesID(userId);
			if (getRolesID > 0)
			{
				TempData["getRolesID"] = getRolesID;
				HttpContext.Session.SetInt32("getRolesID", getRolesID);
			}
			if (userId > 0)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                TempData["ErrorMessage"] = "用户名或密码错误";
                return RedirectToAction("Login");
            }
        }
        public IActionResult UserManagement()
        {
            var users = userService.UserAllAsync();
            ViewData.Model = users;
            return View();
        }
        public IActionResult SelectAll()
        {
            return View();
        }
        public async Task<IActionResult> SelectFY(FenYe2<View_UserRoles> fy)
        {
            fy = await userService.SelectFY(fy);
            return Json(fy);
        }
        public async Task xllb()
        {
            List<Roles> list = await userService.XLRoles();
            SelectList selectListItems = new SelectList(list,"RolesID","RolesName");
            ViewBag.roles = selectListItems;//角色名称的下拉列表
        }
        public async Task<IActionResult> AddUser()//添加页面
        {
            await xllb();
            return View();
        }
        public async Task<IActionResult> Add(View_UserRoles v)//添加
        {
           
            User user = new User();
            user.UName = v.UName;
            user.UTrueName = v.UTrueName;
            user.UPassWord = v.UPassWord;
            int row = await userService.AddUser(user);
            UserRoles userRoles = new UserRoles();
            userRoles.UserID = user.UId;
            userRoles.RolesID = v.RolesID;
			int result = await userService.AddUserRoles(userRoles);
            if(row > 0 && result > 0)
            {
                return RedirectToAction("SelectAll");
            }
            else
            {
                return RedirectToAction("AddUser");
            }
		}
        public async Task<IActionResult> UpdUser(short id)//修改条页面
        {
            View_UserRoles v = await userService.SelectByUId(id);
            ViewData.Model = v;
            await xllb();
            return View();
        }
        public async Task<IActionResult> Update(View_UserRoles v)//修改
        {
			User user = new User();
            user.UId = v.UId;
			user.UName = v.UName;
			user.UTrueName = v.UTrueName;
			user.UPassWord = v.UPassWord;
			int row = await userService.UpdUser(user);
			UserRoles userRoles = new UserRoles();
            userRoles.UserRolesID = v.UserRolesID;
			userRoles.UserID = user.UId;
			userRoles.RolesID = v.RolesID;
			int result = await userService.UpdUserRoles(userRoles);
			if (row > 0 && result > 0)
			{
				return RedirectToAction("SelectAll");
			}
			else
			{
				return RedirectToAction("UpdUser", new {id=v.UId});
			}
		}
        public async Task<IActionResult> DelUser(short id)//删除
        {
            int row = await userService.DelUser(id);
            int result = await userService.DelUserRoles(id);
			if (row > 0 && result > 0)
			{
				return RedirectToAction("SelectAll");
			}
			else
			{
				return RedirectToAction("SelectAll");
			}
		}
	}
}
